package com.hxzy.excel.converter;

import com.alibaba.excel.converters.Converter;
import com.alibaba.excel.enums.CellDataTypeEnum;
import com.alibaba.excel.metadata.GlobalConfiguration;
import com.alibaba.excel.metadata.data.ReadCellData;
import com.alibaba.excel.metadata.data.WriteCellData;
import com.alibaba.excel.metadata.property.ExcelContentProperty;

public class SysUserSexConverter implements Converter<String> {

    /**
     * java转换的时候，数据类型
     *
     * @return
     */
    @Override
    public Class<?> supportJavaTypeKey() {
        return String.class;
    }

    /**
     * excel的数据类型
     *
     * @return
     */
    @Override
    public CellDataTypeEnum supportExcelTypeKey() {
        return CellDataTypeEnum.STRING;
    }

    /**
     * 读取excel数据时候，要把excel中的单元可的值，转换，写入到java实体类中
     *
     * @param cellData
     * @param contentProperty
     * @param globalConfiguration
     * @return
     * @throws Exception
     */
    @Override
    public String convertToJavaData(ReadCellData<?> cellData, ExcelContentProperty contentProperty, GlobalConfiguration globalConfiguration) throws Exception {
        String value = cellData.getStringValue();
        if (value.equals("男")) {
            return "0";
        } else if (value.equals("女")) {
            return "1";
        } else  if (value.equals("未知")) {
            return "2";
        } else {
            return value;
        }

    }

    /**
     * java实体类中读取的值，写入到excel单元格中
     *
     * @param value
     * @param contentProperty
     * @param globalConfiguration
     * @return
     * @throws Exception
     */
    @Override
    public WriteCellData<?> convertToExcelData(String value, ExcelContentProperty contentProperty, GlobalConfiguration globalConfiguration) throws Exception {
        String newValue = "";
        if (value.equals("0")) {
            newValue = "男";
        } else  if (value.equals("1")) {
            newValue = "女";
        } else  if (value.equals("2")) {
            newValue = "未知";
        }
        return new WriteCellData<>(newValue);
    }
}
